/*
 * @Description: 
 * @Author: 张文韬
 * @Date: 2024-06-29 11:44:51
 * @LastEditTime: 2024-07-16 14:25:59
 * @LastEditors: 张文韬
 * @Reference: 
 */
import idCardTool from "@/utils/id-card";
import { dayjs } from "element-plus";

let defaultCardType
let defaultMonry

export const parseInsurant = (text, premium, certificateType = '0') => {
  if (!text || text.length == 0) return
  defaultCardType = certificateType
  defaultMonry = premium
  const  success = [], fails = []
  const data = text.trim().split('\n')
  data.map(o => {
    const sepFlag = o.indexOf('\t') > -1?'\t' : ' '
    const obj = getLineInfo(o.split(sepFlag))
    obj?success.push(obj):fails.push(o)
  })
  return { success, fails }
}

const getLineInfo = (line) => {
  const obj = initInsurant()
  obj.name = getName(line)
  obj.phone = getPhone(line)
  console.log(obj.phone);
  const temp = line.filter(o => o != obj.phone)
  const {certificateType, certificateNo} = getCardNumber(temp)
  obj.certificateType = certificateType
  obj.certificateNo = certificateNo
  if (!certificateNo || !certificateType || (certificateType == '01' && !idCardTool.isIdCard(certificateNo))) return
  else if (certificateType == '0' && idCardTool.isIdCard(certificateNo)) {
    obj.gender = idCardTool.getSex(obj.certificateNo)
    obj.birthday = idCardTool.getBirth(obj.certificateNo)
    obj.age = idCardTool.getAge(obj.certificateNo)
  }
  else {
    const gender = getSex(line)
    if (gender) {
      obj.gender = gender
    }
    const birthday = getBirth(line)
    if (birthday) {
      obj.birthday = birthday
      obj.age = dayjs().diff(dayjs(obj.birthday), 'year')
    }
    else {
      obj.age = getAge(line)
    }
  }
  return obj
}

const getName = (line) => {
  const regName = /^[\u4e00-\u9fa5]{2,5}$/
  if (regName.test(line[0])) {
    return line[0]
  }
  return line.find(o => regName.test(o))
}

const getCardNumber = (line) => {
  const regCardNumber = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
  const regHZ = /^[a-zA-Z0-9]{5,17}$/
  const regJG = /^[a-zA-Z0-9]{6,10}$/
  const certificateNo = line.find(o => regCardNumber.test(o))
  if (certificateNo) {
    return { certificateType: '01', certificateNo }
  }
  const hz = line.find(o => regHZ.test(o))
  if (hz) {
    return { certificateType: '02', certificateNo: hz }
  }
  const jg = line.find(o => regJG.test(o))
  if (jg) {
    return { certificateType: '03', certificateNo: jg }
  }
  return {}
}

const getPhone = (line) => {
  const regPhone = /^1[3456789]\d{9}$/
  return line.find(o => regPhone.test(o))
}

const getSex = (line) => {
  const regSex = /^(男|女)$/
  return line.find(o => regSex.test(o))
}

const getAge = (line) => {
  const regAge = /^[1-9]\d{0,2}$/
  return line.find(o => regAge.test(o))
}

const getBirth = (line) => {
  const regBirth = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
  return line.find(o => regBirth.test(o))
}

const initInsurant = () => {
  return {
    id: dayjs().valueOf(),
    name: '',
    certificateType: defaultCardType,
    certificateNo: '',
    gender: '男',
    birthday: '',
    age: '',
    relationshipWithApplicant: '',
    premium: defaultMonry
  }
}

export default parseInsurant